/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import entities.Candidate;
import entities.User;
import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.application.FacesMessage;

/**
 *
 * @author Alvaro
 */
@ApplicationScoped
public class MessagesHelper {
    
        public static String getMessage(String string,User user) throws IOException{
            String language="es";
            if(user.getUserConfiguration()!=null){
                language=user.getUserConfiguration().getLanguage().getCode();
            }
            Locale locale = new Locale(language);
            ResourceBundle rb = ResourceBundle.getBundle("messages", locale);

            return rb.getString(string);
        } 
        
        public String getMessage(String fileName, String messageName, FacesMessage.Severity severity) throws IOException{
            Properties properties = new Properties();
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName+".properties"));
            return properties.getProperty(messageName);
        }

        
        public static Properties getPropertyFromCandidate(Candidate candidate) throws IOException{
            
            String language="";
            String messageProperties="messages";
            String messageProperties1=".properties";
            if(candidate.getCandidateConfiguration()==null || candidate.getUser().getUserConfiguration()==null){
            }
            else if(candidate.getCandidateConfiguration().getEmailAdquired() &&candidate.getUser().getUserConfiguration()!=null){
               language=candidate.getUser().getUserConfiguration().getLanguage().getCode();
               if(language.equals("en") || language.equals("es")){
                   language="_"+language;
               }
               else{
                   language="";
               }
            }

            Properties properties = new Properties();
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(messageProperties+language+messageProperties1));
            return properties;
        }

        

}
